package simplejson

import (
	"encoding/json"
)

type _Json struct {
	data interface{}
}

func (j *_Json) Data() interface{} {
	return j.data
}
func (j *_Json) GetInterface(key string) interface{} {
	m, ok := j.data.(map[string]interface{})
	if ok {
		v, _ok := m[key]
		if _ok {
			return v
		}
	}
	return nil
}

func (j *_Json) Get(key string) Json {

	return &_Json{data: j.GetInterface(key)}
}
func (j *_Json) Set(key string, v interface{}) {
	mp := j.data.(map[string]interface{})
	m, ok := v.(*_Json)
	if ok {
		mp[key] = m.data
	} else {
		mp[key] = v
	}

}
func convertArr(iarr InterfaceArr)interface{}  {
	for k,v:=range iarr{
		aj,ok := v.(*_ArrayJson)
		if ok{
			iarr[k] = convertArr(aj.InterfaceArr);
		}else{
			mj,ok := v.(map[string]interface{})
			if ok{
				convertMap(mj)
			}else{

			}

		}
	}
	return iarr
}
func convertMap(mp map[string]interface{})  {
	for k,v:=range mp{
		aj,ok := v.(*_ArrayJson)
		if ok{
			mp[k]=convertArr(aj.InterfaceArr);
		}else{
			mj,ok := v.(map[string]interface{})
			if ok{
				convertMap(mj)
			}else{

			}
		}
	}
}
func convert(v interface{})  interface{}{
	aj,ok:= v.(*_ArrayJson)
	if(ok){
		convertArr(aj.InterfaceArr);
		return aj.InterfaceArr
	}else{
		mj,ok := v.(map[string]interface{})
		if ok {
			convertMap(mj);
		}
		return v
	}
}

func (j *_Json) convertForMarshl()  {
	j.data=convert(j.data)
}

func (j *_Json) Marshal() ([]byte, error) {
	j.convertForMarshl()

	return json.Marshal(j.data)

}

func (j *_Json) AsDefNum() float64 {
	vf, ok := j.data.(float64)
	if ok {
		return vf
	}
	return 0
}
func (j *_Json) Bool() bool {
	vi, ok := j.data.(bool)
	if ok {
		return vi
	}
	return false
}
func (j *_Json) Interface() interface{} {
	return j.data
}
func (j *_Json) Int() int {
	vi, ok := j.data.(int)
	if ok {
		return vi
	}
	return int(j.AsDefNum())
}
func (j *_Json) Uint() uint {
	vi, ok := j.data.(uint)
	if ok {
		return vi
	}
	return uint(j.AsDefNum())
}
func (j *_Json) Int64() int64 {
	vi, ok := j.data.(int64)
	if ok {
		return vi
	}
	return int64(j.AsDefNum())
}
func (j *_Json) Uint64() uint64 {
	vi, ok := j.data.(uint64)
	if ok {
		return vi
	}
	return uint64(j.AsDefNum())
}
func (j *_Json) Float32() float32 {

	return float32(j.AsDefNum())
}
func (j *_Json) Float64() float64 {

	return j.AsDefNum()
}
func (j *_Json) String() string {

	vi, ok := j.data.(string)
	if ok {
		return vi
	}
	return ""
}

func (j *_Json) ParseDefNum(d interface{}) float64 {

	if d == nil {
		return 0
	}

	vf, ok := d.(float64)
	if ok {
		return vf
	}
	return 0
}
func asBool(val interface{}) bool {
	if val == nil {
		return false
	}
	vi, ok := val.(bool)
	if ok {
		return vi
	}
	return false
}
func asDefNum(d interface{}) float64 {
	if d == nil {
		return 0
	}
	vf, ok := d.(float64)
	if ok {
		return vf
	}
	return 0
}
func asInt(d interface{}) int {
	if d == nil {
		return 0
	}
	vi, ok := d.(int)
	if ok {
		return vi
	}
	return int(asDefNum(d))
}
func asInt64(d interface{}) int64 {
	if d == nil {
		return 0
	}
	vi, ok := d.(int64)
	if ok {
		return vi
	}
	return int64(asDefNum(d))
}
func asUint(d interface{}) uint {
	if d == nil {
		return 0
	}
	vi, ok := d.(uint)
	if ok {
		return vi
	}
	return uint(asDefNum(d))
}
func asUint64(d interface{}) uint64 {
	if d == nil {
		return 0
	}
	vi, ok := d.(uint64)
	if ok {
		return vi
	}
	return uint64(asDefNum(d))
}
func asFloat64(d interface{}) float64 {
	return asDefNum(d)
}
func asFloat32(d interface{}) float32 {
	if d == nil {
		return 0
	}
	vi, ok := d.(float32)
	if ok {
		return vi
	}
	return float32(asDefNum(d))
}
func asString(d interface{}) string {

	if d == nil {
		return ""
	}
	vi, ok := d.(string)
	if ok {
		return vi
	}
	return ""
}
func (j *_Json) Exist(key string) bool {
	return j.GetInterface(key) != nil
}
func (j *_Json) GetBool(key string) bool {
	return asBool(j.GetInterface(key))
}
func (j *_Json) GetInt(key string) int {
	return asInt(j.GetInterface(key))
}
func (j *_Json) GetUint(key string) uint {
	return asUint(j.GetInterface(key))
}
func (j *_Json) GetInt64(key string) int64 {
	return asInt64(j.GetInterface(key))
}
func (j *_Json) GetUint64(key string) uint64 {
	return asUint64(j.GetInterface(key))

}
func (j *_Json) GetFloat32(key string) float32 {
	return asFloat32(j.GetInterface(key))
}
func (j *_Json) GetFloat64(key string) float64 {
	return asFloat64(j.GetInterface(key))
}
func (j *_Json) GetString(key string) string {

	return asString(j.GetInterface(key))
}
